<?php
namespace app\admin\controller;
use think\Controller;
class Login extends Controller{
    
    public function index(){
        if(request()->isPost()){
          $data=input("post.");  
          //print_r($data);exit;
          $username=$data["username"];
          $password=$data["password"];
          if(!$username){
           return   showmiss(0,"用户名不能为空");
          }
          if(!$password){
            return   showmiss(0,"密码不能为空");
          }
          
          $ret=model("Admin")->get(["username"=>$data["username"]]);
          
          if(!$ret){
           return   showmiss(0,"该用户不存在");
          }
          //验证密码
          if($ret->password !=md5($data['password'].$ret->code)){
            return    showmiss(0,"密码不一致");
          }
          //保存数据
          model("Admin")->updateById(["last_login_time"=>time()],$ret->aid);
          //session保存数据
          session("Admin",$ret,"admin");
          return showmiss(1,"登陆成功");
            
        }else{
             $admin=session("Admin","","admin");
            if($admin  && $admin->aid){
                $this->redirect(url("index/index"));
            }
        
         return $this->fetch();
        }
    }
    //退出
    public function logout(){
        //清除session
        session(null,"admin");
        $this->redirect(url("login/index")); 
    }
    
}

